package com.tengju.data.infrastructure.repository.abm;

import com.google.common.collect.Lists;
import com.tengju.data.domain.model.user.DealerInfo;
import com.tengju.data.domain.model.user.DealerInfoRepository;
import com.tengju.data.domain.model.user.UserId;
import com.tengju.data.infrastructure.persistence.mybatis.dataobject.DealerInfoDO;
import com.tengju.data.infrastructure.persistence.mybatis.mapper.data.custom.DealerInfoDOMapper;
import com.tengju.data.infrastructure.repository.convert.DealerInfoConvert;
import com.tengju.user.domain.model.user.UserIdCode;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 李大仙
 * @Date: 2021/3/24 3:35 下午
 */
@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class DealerInfoRepositoryImpl implements DealerInfoRepository {

    private final DealerInfoDOMapper dealerInfoDOMapper;

    @Override
    public List<DealerInfo> pageQuery(UserId userId, Integer offset, Integer limit) {
        List<DealerInfoDO> list = dealerInfoDOMapper.pageQuery(userId.getValue(), offset, limit);
        return list.stream().map(DealerInfoConvert::convert)
                .collect(Collectors.toList());
    }

    @Override
    public DealerInfo getById(UserId userId) {
        DealerInfoDO dealerInfoDO = dealerInfoDOMapper.selectById(userId.getValue());
        return DealerInfoConvert.convert(dealerInfoDO);
    }

    @Override
    public List<DealerInfo> getByIds(List<UserId> userIds) {
        if (CollectionUtils.isEmpty(userIds)) {
            return Lists.newArrayList();
        }
        List<Long> collect = userIds.stream().map(UserId::getValue).collect(Collectors.toList());
        List<DealerInfoDO> dealerInfoDOList = dealerInfoDOMapper.selectByIds(collect);
        return dealerInfoDOList.stream()
                .map(DealerInfoConvert::convert)
                .collect(Collectors.toList());
    }
}
